package com.wanmait.teatrain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanmait.teatrain.entity.HupRegion;
import com.wanmait.teatrain.entity.TrainingTag;
import com.wanmait.teatrain.mapper.TrainingTagMapper;
import com.wanmait.teatrain.service.TrainingTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.teatrain.utils.AjaxResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 培训内容标签表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2025-10-22
 */
@Service
public class TrainingTagServiceImpl extends ServiceImpl<TrainingTagMapper, TrainingTag> implements TrainingTagService {

    @Resource
    private TrainingTagMapper trainingTagMapper;

    //获得标签数据，格式为级联选择器所需的数据格式 --徐钰淞
    @Override
    public AjaxResult getTag() {
        QueryWrapper<TrainingTag> queryWrapper = new QueryWrapper<>();
        List<TrainingTag> trainingTags = trainingTagMapper.selectList(queryWrapper);
        List<Map<String,Object>> result = new ArrayList<>();
        for (TrainingTag trainingTag : trainingTags) {
            System.out.println(trainingTag);
            if ( trainingTag.getPid() == null ) {
                Map<String,Object> tagmap = new HashMap<>();
                tagmap.put("value",trainingTag.getId());
                tagmap.put("label",trainingTag.getTagName());
                tagmap.put("disables",true);
                tagmap.put("children",getChildren(trainingTags,trainingTag.getId()));
                result.add(tagmap);
            }
        }
        return AjaxResult.success(result);
    }
    //获得标签级联菜单的子菜单 --徐钰淞
    public List<Map<String,Object>> getChildren(List<TrainingTag> trainingTags, Integer pid) {
        List<Map<String, Object>> children = new ArrayList<>();
        for (TrainingTag trainingTag : trainingTags) {
            if (trainingTag.getPid() != null ){
                if (trainingTag.getPid().equals(pid)) {
                    Map<String,Object> childMap = new HashMap<>();
                    childMap.put("value",trainingTag.getId());
                    childMap.put("label",trainingTag.getTagName());
                    children.add(childMap);
                }
            }

        }
        return children;
    }
}
